import sqlite3

def add_user_id_column():
    """向investment_history表添加user_id列"""
    try:
        # 连接数据库
        with sqlite3.connect('investment_history.db') as conn:
            cursor = conn.cursor()
            
            # 检查表结构，看是否已经有user_id列
            cursor.execute("PRAGMA table_info(investment_history)")
            columns = [column[1] for column in cursor.fetchall()]
            
            if 'user_id' not in columns:
                # 添加user_id列，设置默认值为1（admin用户）
                cursor.execute("ALTER TABLE investment_history ADD COLUMN user_id INTEGER NOT NULL DEFAULT 1")
                conn.commit()
                print("成功向investment_history表添加user_id列")
            else:
                print("investment_history表已经包含user_id列")
                
    except sqlite3.Error as e:
        print(f"修改表结构时发生错误: {e}")
    except Exception as e:
        print(f"发生未知错误: {e}")

if __name__ == '__main__':
    add_user_id_column()